package com.pxmeta.admin.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.pxmeta.common.core.domain.AjaxResult;
import com.pxmeta.common.utils.SecurityUtils;
import com.pxmeta.party.entity.*;
import com.pxmeta.party.entity.dto.AddGiftDTO;
import com.pxmeta.party.entity.dto.UpdateAssociationUserRoleDTO;
import com.pxmeta.party.entity.dto.UpdatePartyDTO;
import com.pxmeta.party.entity.vo.GiftLotteryVO;
import com.pxmeta.party.service.*;
import com.pxmeta.user.entity.User;
import com.pxmeta.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(value = "房间管理",tags = "房间")
@CrossOrigin
@RequestMapping(value = "/party")
@RequiredArgsConstructor
public class PartyController {


    private final IPartyService partyService;
    private final IGiftService giftService;
    private final IUserService userService;

    private final ILotteryLuckyUserService lotteryLuckyUserService;

    private final IAssociationUserService associationUserService;

    private final IGiftLotteryService giftLotteryService;

    @GetMapping("/getPartyList")
    @ApiOperation("获取房间列表")
    public AjaxResult<List<Party>> getPartyList(@RequestParam(required = false) String phone,
                                                @RequestParam Integer page,
                                                @RequestParam Integer size,
                                                @RequestParam(required = false) String keyword){

        Long associationId = null;
        if (StringUtils.isNotBlank(phone)){
            User user = userService.getByPhone(phone);
            associationId = user.getAssociationId();
        }
        IPage<Party> result = partyService.getAssociationPartyList(page, size, associationId,keyword);
        return AjaxResult.successPage(result.getRecords(), result.getTotal());
    }

    @GetMapping("/getSigningList")
    @ApiOperation("获取成员列表")
    public AjaxResult getSigningList(@RequestParam(required = false) String phone,
                                    @RequestParam(required = false) Integer status,
                                     @RequestParam Integer page,
                                     @RequestParam Integer size){

        User user = userService.getByPhone(phone);
        Long associationId = user.getAssociationId();
        IPage<AssociationUser> result = associationUserService.getSigningList(associationId, status, page, size);
        return AjaxResult.successPage(result.getRecords(), result.getTotal());
    }

    @PutMapping("/updateParty")
    @ApiOperation("关闭房间")
    public AjaxResult updateParty(@RequestBody UpdatePartyDTO dto){

        String id = dto.getId();
        Party party = partyService.getById(id);
        if (dto.getIsOpen() != null){
            party.setIsHidden(!dto.getIsOpen());
        }
        partyService.updateById(party);
        return AjaxResult.success();
    }

    @GetMapping("/getSigningListByAssociationId")
    @ApiOperation("获取成员列表")
    public AjaxResult getSigningList(@RequestParam(required = false) Long associationId,
                                     @RequestParam(required = false) Integer status,
                                     @RequestParam Integer page,
                                     @RequestParam Integer size){
        IPage<AssociationUser> result = associationUserService.getSigningList(associationId, status, page, size);
        return AjaxResult.successPage(result.getRecords(), result.getTotal());
    }

    @DeleteMapping("/deletePartyById/{id}")
    @ApiOperation("根据id删除派对")
    public AjaxResult deletePartyById(@PathVariable String id){
        partyService.removeById(id);
        return AjaxResult.success();
    }


    @PostMapping("/addGift")
    @ApiOperation("添加礼物")
    public AjaxResult addGift(@RequestBody AddGiftDTO dto){
        giftService.addGift(dto);
        return AjaxResult.success();
    }


    @DeleteMapping("/deleteGiftById/{id}")
    @ApiOperation("删除礼物")
    public AjaxResult deleteGiftById(@PathVariable Long id){
        giftService.removeById(id);
        return AjaxResult.success();
    }

    @PutMapping("/hiddenGift/{id}")
    @ApiOperation("关闭礼物")
    public AjaxResult hiddenGift(@PathVariable Long id){
        Gift gift = giftService.getById(id);
        gift.setStatus(0);
        giftService.updateById(gift);
        return AjaxResult.success();
    }

    @PutMapping("/showGift/{id}")
    @ApiOperation("开启礼物")
    public AjaxResult showGift(@PathVariable Long id){
        Gift gift = giftService.getById(id);
        gift.setStatus(1);
        giftService.updateById(gift);
        return AjaxResult.success();
    }

    @PutMapping("/setRole")
    @ApiOperation("修改用户权限")
    public AjaxResult updateRole(@RequestBody UpdateAssociationUserRoleDTO dto){
        AssociationUser associationUser = associationUserService.getUserSign(dto.getUserId());
        if (dto.getRole() == 1){
            associationUser.setIsAdmin(Boolean.TRUE);
        }else {
            associationUser.setIsAdmin(Boolean.FALSE);
        }
        associationUserService.updateById(associationUser);

        return AjaxResult.success();
    }


    @PutMapping("/updateGift")
    @ApiOperation("修改礼物")
    public AjaxResult updateGift(@RequestBody Gift gift){
        giftService.updateById(gift);
        return AjaxResult.success();
    }


    @GetMapping("/getLuckyUsers")
    @ApiOperation("获取中奖记录")
    public AjaxResult getLuckyUsers(@RequestParam Integer page,
                                    @RequestParam Integer size){
        LambdaQueryWrapper<LotteryLuckyUser> wrapper = Wrappers.lambdaQuery();
        wrapper.orderByDesc(LotteryLuckyUser::getCreateTime);
        Page<LotteryLuckyUser> result = lotteryLuckyUserService.page(new Page<>(page, size), wrapper);
        return AjaxResult.successPage(result.getRecords(),result.getTotal());
    }


    @GetMapping("/getLotteryProbability")
    @ApiOperation("获取抽奖概率")
    public AjaxResult getLotteryProbability(@RequestParam Integer type){
        List<GiftLotteryVO> list = Lists.newLinkedList();
        if (type == 1){ //宝箱
             list = giftService.getRewardList(9999, Boolean.TRUE);
        }else if (type == 2){//欢乐转盘
            list = giftService.getRewardList(2000,Boolean.TRUE);
        }else if (type == 3){ //黄金转盘
            list = giftService.getRewardList(1000,Boolean.TRUE);
        }
        return AjaxResult.successPage(list, (long) list.size());
    }


    @PutMapping("/updateLottery")
    @ApiOperation("修改概率")
    public AjaxResult updateProbability(@RequestBody GiftLottery giftLottery){
        giftLotteryService.updateById(giftLottery);
        return AjaxResult.success();
    }







}
